package com.hopoke.common.domain;

import com.hopoke.common.expection.CommonException;
import lombok.Data;

@Data
public class Result<T> {
    private int code;
    private String message;
    private T data;

    public static <T> Result<T> ok(T data){
        return build(ResultCodeEnum.SUCCESS.getCode(), data, ResultCodeEnum.SUCCESS.getMessage());
    }

    public static <T> Result<T> ok(){
        return build(ResultCodeEnum.SUCCESS.getCode(), null, ResultCodeEnum.SUCCESS.getMessage());
    }

    public static <T> Result<T> error(CommonException e) {
        return build(e.getCode(), null, e.getMessage());
    }

    public static <T> Result<T> build(int code, T data, String message){
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setData(data);
        result.setMessage(message);
        return result;
    }
}
